package com.example.simple_cheat;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.List;

public class Myadapter extends RecyclerView.Adapter <Myadapter.ViewHolder>{
    private List<MSG> message_list;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MSG msg = message_list.get(position);
        if(msg.getType() == MSG.TYPE_received){
            holder.leftlayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMeg.setText(msg.getContent());
            holder.Friendname.setText(msg.getFriendname());
        }
        else{
            holder.leftlayout.setVisibility(View.GONE);
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.RightMeg.setText(msg.getContent());
            holder.Datasend.setText(msg.getDate());
        }
    }

    @Override
    public int getItemCount() {
       return message_list.size();
    }

    static  class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftlayout;
        LinearLayout rightLayout;
        TextView leftMeg;
        TextView RightMeg;
        TextView Friendname;
        TextView Datasend;
        TextView Datareceived;
        public  ViewHolder(View view){
            super(view);
            Friendname = (TextView)view.findViewById(R.id.Username);
            leftlayout = (LinearLayout)view.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout)view.findViewById(R.id.right_layout);
            leftMeg = (TextView)view.findViewById(R.id.left_text);
            RightMeg = (TextView)view.findViewById(R.id.right_text);
            Datareceived = (TextView)view.findViewById(R.id.date);
            Datasend = (TextView)view.findViewById(R.id.datesent);
        }
    }
    public Myadapter(List<MSG> msgList){
        message_list = msgList;
    }

}
